package com.template.api.service.config;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.template.api.entity.po.ConfigBanner;
import com.template.api.entity.vo.ResultConfigBannerVO;
import com.template.api.mapper.ConfigBannerMapper;
import com.template.common.constant.ConstantPool;
import com.template.common.enums.SysStatusEnum;
import com.template.common.utils.RedisUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author mltong
 */
@Service
public class CliConfigBannerService extends ServiceImpl<ConfigBannerMapper, ConfigBanner> {

    @Resource
    private RedisUtil redisUtil;
    private static final Lock REEN_LOCK = new ReentrantLock();

    public List<ResultConfigBannerVO> listBanner(String scope) {
        String resData = redisUtil.get(ConstantPool.CACHE_KEY_BANNER + scope);
        if(StringUtils.isEmpty(resData)){
            if (REEN_LOCK.tryLock()) {
                try{
                    List<ResultConfigBannerVO> bannerData = new ArrayList<>();
                    LambdaQueryWrapper<ConfigBanner> queryWrapper = new LambdaQueryWrapper<ConfigBanner>()
                            .eq(ConfigBanner::getStatus, SysStatusEnum.YES.getStatus())
                            .eq(ConfigBanner::getDelStatus, SysStatusEnum.YES.getStatus())
                            .eq(ConfigBanner::getScope, scope)
                            .orderByAsc(ConfigBanner::getSort);
                    List<ConfigBanner> configBanners = list(queryWrapper);
                    for(ConfigBanner configBanner : configBanners){
                        ResultConfigBannerVO sysBannerData = new ResultConfigBannerVO();
                        sysBannerData.setImageUrl(configBanner.getImageUrl());
                        sysBannerData.setDataGroup(configBanner.getDataGroup());
                        sysBannerData.setSort(configBanner.getSort());
                        sysBannerData.setRoute(configBanner.getRoute());
                        sysBannerData.setTitle(configBanner.getTitle());
                        bannerData.add(sysBannerData);
                    }
                    resData = JSONObject.toJSONString(bannerData);
                    redisUtil.set(ConstantPool.CACHE_KEY_BANNER + scope, resData, ConstantPool.CACHE_TIME_1_YEAR);
                }finally {
                    REEN_LOCK.unlock();
                }
            }else{
                try { TimeUnit.MILLISECONDS.sleep(ConstantPool.SPIN_LOCK_SLEEP_TIME); } catch (InterruptedException e) {e.printStackTrace();}
                resData = redisUtil.get(ConstantPool.CACHE_KEY_BANNER + scope);
                if(StringUtils.isEmpty(resData)){
                    return listBanner(scope);
                }
            }
        }

        return JSONArray.parseArray(resData, ResultConfigBannerVO.class);
    }
}
